package com.niuke;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

/**
 * 23. 服务失效判断
 * https://zhuanlan.zhihu.com/p/526678876?utm_id=0
 */
public class NiukeMoni23 {

    public static Map<String, Set<String>> yilai;
    public static Set<String> result;

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        String[] data = scanner.nextLine().split(",");
        String[] guzhang = scanner.nextLine().split(",");

        result = new HashSet<>();
        yilai = new HashMap<>();
        for (int i = 0; i < data.length; i++) {
            String[] item = data[i].split("-");
            result.add(item[0]);
            result.add(item[1]);

            Set<String> value = yilai.getOrDefault(item[1], new HashSet<>());
            value.add(item[0]);
            yilai.put(item[1], value);
        }

        for (int i = 0; i < guzhang.length; i++) {
            result.remove(guzhang[i]); // 先移除故障的起始
            dfs(guzhang[i]); // 再递归移除因依赖它而故障的对象
        }

        System.out.println(Arrays.toString(result.toArray()));
    }

    public static void dfs(String key) {
        Set<String> value = yilai.get(key);
        if (value != null && value.size() > 0) {
            System.out.println(key + " --要移除-- " + Arrays.toString(value.toArray()));
            result.removeAll(value);
            System.out.println(key + " --移除后-- " + Arrays.toString(result.toArray()));
            yilai.remove(key);
            for (String subKey : value) {
                dfs(subKey);
            }
        }
    }

}
